package com.chen.dao.impl;

import com.chen.dao.ShopcarDao;
import com.chen.entity.Comment;
import com.chen.entity.News;
import com.chen.entity.Shopcart;
import com.chen.entity.Trolley;
import com.chen.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class ShopcarDaoImpl implements ShopcarDao {

    Connection conn = DBUtil.getConnection();
    QueryRunner qr = new QueryRunner();

    @Override
    public int save(Shopcart shopcart) {
        String sql = "INSERT INTO  tb_shopcart(shopcart_id,goods_id,goods_name,goods_price," +
                    " goods_img,goods_num,user_id) VALUES (?,?,?,?,?,?,?)";
        Object[] paras = {shopcart.getShopcart_id(),shopcart.getGoods_id(),shopcart.getGoods_name(),shopcart.getGoods_price(),
                        shopcart.getGoods_img(),shopcart.getGoods_num(),shopcart.getUser_id()};
        Comment comment = null;
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Shopcart shopcart) {
        String sql = "UPDATE  tb_shopcart SET  goods_id=?,goods_name=?," +
                "goods_price=?,goods_img=?,goods_num=?,user_id=? WHERE shopcart_id=?  ";
        Object[] paras = {shopcart.getGoods_id(),shopcart.getGoods_name(),shopcart.getGoods_price(),
                shopcart.getGoods_img(),shopcart.getGoods_num(),shopcart.getUser_id(),shopcart.getShopcart_id()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Object id) {
        String sql = " DELETE FROM tb_shopcart WHERE shopcart_id  = ?  ";
        Object[] paras = {id};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Shopcart findById(Object key) {
        String sql=" SELECT * FROM tb_shopcart where shopcart_id  = ? ";
        Object[] paras={key};
        try {
            Shopcart shopcart=qr.query(conn,sql,paras,new BeanHandler<Shopcart>(Shopcart.class));
            return shopcart;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Shopcart> findAll() {
        String sql = "SELECT * FROM tb_shopcart";
        Object[] paras = {};
        try {
            List<Shopcart> shopcarts = qr.query(conn, sql, paras, new BeanListHandler<Shopcart>(Shopcart.class));
            return shopcarts;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Shopcart> findSearch(String keyword) {
        return null;
    }

    @Override
    public List<Shopcart> findPage(int page, int pagesize) {
        return null;
    }

    @Override
    public long count() {
        return 0;
    }

    @Override
    public Shopcart findByGoodsId(String id) {
        String sql=" SELECT * FROM tb_shopcart where user_id  = ? ";
        Object[] paras={id};
        try {
            Shopcart shopcart=qr.query(conn,sql,paras,new BeanHandler<Shopcart>(Shopcart.class));
            return shopcart;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Shopcart> findByUserId(String id) {
        String sql=" SELECT * FROM tb_shopcart where user_id  = ? ";
        Object[] paras = {id};
        try {
            List<Shopcart> shopcarts = qr.query(conn, sql, paras, new BeanListHandler<Shopcart>(Shopcart.class));
            return shopcarts;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Shopcart findGoodsUserId(String userId, String goodsId) {
        String sql=" SELECT * FROM tb_shopcart where user_id  = ? and goods_id = ?";
        Object[] paras={userId,goodsId};
        try {
            Shopcart shopcart=qr.query(conn,sql,paras,new BeanHandler<Shopcart>(Shopcart.class));
            return shopcart;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
